package com.cross.plain;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import com.cross.core.AbstractReader;
import com.cross.core.Column;
import com.cross.core.Header;
import com.cross.core.Item;
import com.cross.core.Row;
import com.cross.util.Constant;
import com.cross.util.StringUtils;
/**
 * 文本读取器
 * @author wu
 *
 */
public class SimplePlainReader extends AbstractReader {

	private File file;
	private String split = Constant.COLUMNSPLIT;
	private LineNumberReader reader;

	public SimplePlainReader(File file, String split) throws FileNotFoundException {
		this.file = file;
		init();
	}

	public SimplePlainReader(File file) throws FileNotFoundException {
		this.file = file;
		init();
	}

	private void init() throws FileNotFoundException {
		reader = new LineNumberReader(new FileReader(file));
		header = createHeader();
	}

	@Override
	protected boolean nextRow() {
		return nextRowImpl();
	}

	private boolean nextRowImpl() {
		String line = null;
		try {
			line = reader.readLine();
		} catch (IOException e) {
			return false;
		}
		if(line == null){
			return false;
		}
		if (StringUtils.isBlank(line)) {
			return nextRowImpl();
		}
		currentRow = toRow(line);
		return null != currentRow;
	}

	private Header createHeader(){
		String line = null;
		Header header = new Header();
		try {
			line = reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException("实体名不合规范");
		}
		header.setObjectName(line);
		if (StringUtils.isBlank(line)) {
			throw new RuntimeException("实体名不合规范");
		}
		try {
			line = reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException("实体名不合规范");
		}
		if (StringUtils.isBlank(line)) {
			throw new RuntimeException("实体名不合规范");
		}
		
		String[] titles = line.split(split);
		for(int i=0; i<titles.length; i++){
			header.add(toColumn(i, titles[i]));
		}
		return header;
	}
	
	private Column toColumn(int index, String fieldStr){
		if (StringUtils.isBlank(fieldStr)) {
			throw new RuntimeException("列名不合规范");
		}
		int begin = fieldStr.indexOf("(");
		if(begin == -1){
			throw new RuntimeException("列名不合规范");
		}
		int end = fieldStr.indexOf(")", begin);
		if(end == -1){
			throw new RuntimeException("列名不合规范");
		}
		String title = fieldStr.substring(0, begin);
		String structStr = fieldStr.substring(begin + 1, end);
		
		String[] field = structStr.split(Constant.COLDESSPLIT);
		if(field == null || field.length!=3){
			throw new RuntimeException("列名不合规范");
		}
		int precision = Integer.parseInt(field[1]);
		boolean variable = Boolean.parseBoolean(field[2]);
		Column c = new Column(index, title, field[0], precision, variable);
		return c;
	}
	
	private Row toRow(String line) {
		Row row = new Row();
		String[] items = line.split(split);
		for(int index = 0; index < header.size() && index < items.length; index ++){
			row.append(new Item(items[index], header.getColumn(index).getType(), header.getColumn(index).getPrecision()));
		}
		return row;
	}

	@Override
	public void close() {
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
